﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_EditProduct : System.Web.UI.Page
{
    CategoryDAL category = new CategoryDAL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["adminlogin"] == null)
        {
            Response.Redirect("adminlogin.aspx");
        }
        if (!Page.IsPostBack)
        {
            if (Request.QueryString.Count > 0)
            {
                int pdID = Convert.ToInt32(Request.QueryString["ID"]);
                DataSet2TableAdapters.tbl_productTableAdapter daProduct = new DataSet2TableAdapters.tbl_productTableAdapter();
                DataSet2 ds2 = new DataSet2();
                daProduct.FillByID(ds2.tbl_product, pdID);
                DataTable dt = new DataTable();
                dt = ds2.tbl_product;
                FormViewProduct.DataSource = dt;
                FormViewProduct.DataBind();
            }
        }
        
    }
    protected void btnAddProduct_Click(object sender, EventArgs e)
    {
        FileUpload filupload = (FileUpload)FormViewProduct.FindControl("FileUploadImageProduct");
        DropDownList dllCategory = (DropDownList)FormViewProduct.FindControl("DropDownListCategory");
        TextBox txtName = (TextBox) FormViewProduct.FindControl("txtProductName");
        TextBox txtDescription = (TextBox)FormViewProduct.FindControl("txtDescription");
        TextBox txtQuantity = (TextBox) FormViewProduct.FindControl("txtQuantity");
        TextBox txtPrice = (TextBox) FormViewProduct.FindControl("txtPrice");
        ProductBus productBus = new ProductBus();
        CategoryDAL categoryDAL = new CategoryDAL();
        Product objProduct = new Product();
        //lay id cua category
        DataTable dt = new DataTable();
        dt = categoryDAL.GetCategoryID(dllCategory.SelectedItem.ToString());
        int CategoryID = Convert.ToInt32(dt.Rows[0]["cat_id"].ToString());

        //set gia tri
        objProduct.PdID = Convert.ToInt32(Request.QueryString["ID"]);
        objProduct.CatID = CategoryID;
        objProduct.PdName = txtName.Text;
        objProduct.PdDescription = txtDescription.Text;
        objProduct.PdQuantity = Convert.ToInt32(txtQuantity.Text);
        objProduct.PdPrice = Convert.ToDecimal(txtPrice.Text);
        objProduct.PdImage = "../images/" + filupload.FileName;
        filupload.SaveAs(Server.MapPath("~/images/" + filupload.FileName));
        productBus.UpdateProduct(objProduct);
    }
}
